package com.api.shopapi.mapper;

import com.api.shopapi.entity.DeviceYjgateway;
import com.api.shopapi.entity.dto.DeviceYjgatewayDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface DeviceYjgatewayMapper {

    @Insert("insert into device_yjgateway (DeviceYjgatewayCode,UserCode,GatewayId,BindTime) values " +
            "(#{deviceYjgateway.DeviceYjgatewayCode},#{deviceYjgateway.UserCode},#{deviceYjgateway.GatewayId},#{deviceYjgateway.BindTime})")
    int insert(@Param("deviceYjgateway") DeviceYjgateway deviceYjgateway);



    @Select("select * from device_yjgateway where GatewayId=#{gatewayId} limit 1")
    DeviceYjgateway getByGatewayId(@Param("gatewayId") String gatewayId);

    @Select("select * from device_yjgateway where UserCode=#{userCode} limit 1")
    DeviceYjgateway getByUserCode(@Param("userCode") String userCode);

    @Update("update device_yjgateway set UserCode=#{deviceYjgateway.UserCode}," +
            "GatewayId=#{deviceYjgateway.GatewayId},BindTime=#{deviceYjgateway.BindTime} " +
            "where DeviceYjgatewayCode=#{deviceYjgateway.DeviceYjgatewayCode}")
    int update(@Param("deviceYjgateway") DeviceYjgateway deviceYjgateway);


    @Select("<script>" +
            "select dy.*,u.Name as UserName,u.Phone  from device_yjgateway dy left join user u on dy.UserCode = u.UserCode " +
            "left join user_device ud on ud.UserCode = dy.UserCode " +
            "where dy.GatewayId is not null " +
            "and ud.YJGateway = 1 " +
            "<if test ='phone !=null and phone!=\"\" '>" +
            " and u.Phone like concat('%',#{phone},'%') " +
            "</if>" +
            "<if test ='gatewayId !=null and gatewayId!=\"\" '>" +
            " and dy.GatewayId =#{gatewayId} " +
            "</if>" +
            "</script>")
    List<DeviceYjgatewayDTO> getPage(@Param("phone") String phone, @Param("gatewayId") String gatewayId);

    

}
